from rest_framework import filters
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin, CreateModelMixin, UpdateModelMixin, DestroyModelMixin

from apps.models import Classification
from apps.serializers.Classification import ClassificationSerializer
from apps.utils.common_mixin import APIListModelMixin, APICreateModelMixin
from apps.utils.common_response import APIResponse
from apps.views.main.user import make_login_log


class AdminClassificationListView(GenericViewSet, APIListModelMixin, APICreateModelMixin):
    queryset = Classification.objects.all()
    serializer_class = ClassificationSerializer


class AdminClassificationUpdateView(GenericViewSet):
    def create(self, request):
        try:
            id = request.GET.get('id')
            Classification_data = Classification.objects.filter(id=id).first()
        except Classification.DoesNotExist:
            make_login_log(request)
            return APIResponse(code=1, msg='对象不存在')
        serializer = ClassificationSerializer(Classification_data, data=request.data)
        if serializer.is_valid():
            serializer.save()
        return APIResponse(code=0, msg='更新成功', data=serializer.data)


class AdminClassificationDeleteView(GenericViewSet):
    def create(self, request):
        try:
            ids = request.GET.get('ids')
            ids_arr = ids.split(',')
            # 批量删除
            Classification.objects.filter(id__in=ids_arr).delete()
        except Classification.DoesNotExist:
            make_login_log(request)
            return APIResponse(code=1, msg='对象不存在')
        return APIResponse(code=0, msg='删除成功')
